package com.wtwd.device.service.impl;


import com.wtwd.device.constant.Constant;
import com.wtwd.device.dao.MedalMapper;
import com.wtwd.device.model.entity.AppUserMedal;
import com.wtwd.device.model.entity.Medal;
import com.wtwd.device.model.entity.MedalVo;
import com.wtwd.device.service.MedalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author mjy
 * @date 2021/09/23
 * @description
 */
@Service
public class MedalServiceImpl implements MedalService {


    @Autowired
    private MedalMapper medalMapper;

    @Override
    public List<Medal> getUserNotBoundMedal(Long userId, int medalType) {
        return medalMapper.getUserNotBoundMedal(userId, medalType);
    }

    @Override
    public Integer bathAddUserMedal(List<AppUserMedal> bathAddMedalList) {
        return medalMapper.bathAddUserMedal(bathAddMedalList);
    }

    @Override
    public Map<Integer, List<MedalVo>> getUserAllMedal(Long userId) {
        Map<Integer, List<MedalVo>> collect = null;
        List<MedalVo> userAllMedal = medalMapper.getUserAllMedal(userId);
        //根据勋章类型进行分组1.步数，2.跑步，3.骑行，4热量
        if (userAllMedal != null && userAllMedal.size() > 0) {
            userAllMedal.stream().forEach(medalVo -> {
                if (medalVo.getOwn() != null) {
                    medalVo.setOwn(Constant.MEDAL_OWN);//有权
                } else {
                    medalVo.setOwn(Constant.MEDAL_NOOWN);//无权
                }
            });
            collect = userAllMedal.stream().collect(Collectors.groupingBy(MedalVo::getType));
        }
        return collect;
    }

    @Override
    public boolean deleteUserMedalByUserId(Long userId) {
        return medalMapper.deleteUserMedal(userId) == 1;
    }


}
